function jumpContact() {
    window.location="/html/contact.html";
}

function openPicture() {
    var src=$(this).attr("src");
    var pid=$(this).attr("pid");
    $(".img-modal").find("img").attr("src",src);
    $(".img-modal").find("img").attr("pid",pid);
}

//选择型分页条
function page(page,number,parent) {
    //生成最大最小页码
    var maxPage=parseInt(page)+3;
    var minPage=parseInt(page)-3;
    if(minPage<1){
        maxPage+=1-minPage;
        minPage=1;
    }

    if(maxPage>Math.ceil(number/8)){
        minPage-=maxPage-Math.ceil(number/8);
        maxPage=Math.ceil(number/8);
        if(minPage<1){
            minPage=1;
        }
    }

    var html="   <nav aria-label='Page navigation'>\n" +
        "                    <ul class='pagination'>\n" +
        "                        <li>\n" +
        "                            <a aria-label='Previous'>\n" +
        "                                <span aria-hidden='true'>&laquo;</span>\n" +
        "                            </a>\n" +
        "                        </li>";

    for(var i=minPage;i<=maxPage;i++){
        if(i==page){
            html+="<li class='active'><a>"+i+"</a></li>";
        }else {
            html+="<li><a>"+i+"</a></li>";
        }
    }

    html+="<li>\n" +
        "                            <a aria-label='Next'>\n" +
        "                                <span aria-hidden='true'>&raquo;</span>\n" +
        "                            </a>\n" +
        "                        </li>\n" +
        "                    </ul>\n" +
        "                </nav>";

    parent.html(html);
}

//分页栏
function flushTable(url,json,pageEle,parentEle) {
    json=JSON.parse(json);
    var pageIndex=json.page;

    //发送前先序列化json
    for(var key in json){
        if(typeof (json[key])!='string'){
            json[key]=JSON.stringify(json[key]);
        }
    }
    $.get(url,json,function (data) {
        //对json进行反序列化
        //发送前先序列化json
        for(var key in json){
            json[key]=JSON.parse(json[key]);
        }

        //获取data并清空table
        var parent=$(parentEle);
        parent.empty();

        //重做导航栏
        page(json.page,data.number,$(pageEle));
        var options=$(".pagination li");
        json.page=1;
        options.get(0).setAttribute("onclick","flushTable('"+url+"','"+JSON.stringify(json)+"','"+pageEle+"','"+parentEle+"')");
        for(var i=1;i<options.length-1;i++){
            json.page=options.get(i).getElementsByTagName("a")[0].innerHTML;
            options.get(i).setAttribute("onclick","flushTable('"+url+"','"+JSON.stringify(json)+"','"+pageEle+"','"+parentEle+"')");
        }
        json.page=Math.ceil(data.number/8);

        options.get(options.length-1).setAttribute("onclick","flushTable('"+url+"','"+JSON.stringify(json)+"','"+pageEle+"','"+parentEle+"')");

        //给table新增相关的项
        data=data.data;
        for(var i=0;i<data.length;i++){
            addOne(data[i]);
        }
    });
}

//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

//获取city
function getCity(select) {
    $.ajaxSetup({
        async:false
    });
    $.get("/home/allCity",function (data) {
        for(var i=0;i<data.length;i++){
            var option="<option value='"+data[i].id+"'>"+data[i].data+"</option>";
            select.append(option);
        }
    });
}

//获取language
function getLanguage(select) {
    $.ajaxSetup({
        async:false
    });
    $.get("/home/allLanguage",function (data) {
        for(var i=0;i<data.length;i++){
            var option="<option value='"+data[i].id+"'>"+data[i].data+"</option>";
            select.append(option);
        }
    });
}

//获取gallery
function getGallery() {
    $.get("/home/selectGallery",{id:getQueryString("id")},function (data) {
        $(".img-picture").empty();
        for(var i=0;i<data.length;i++){
            var img=$("<img pid='"+data[i].id+"' class='img-thumbnail img-card' src='"+data[i].data+"'>");
            img.click(openPicture);
            img.attr("data-toggle","modal");
            img.attr("data-target","#modal-img");
            $(".img-picture").append(img);
        }
    });
}

//获取reviews
function getReviews(type) {
    $.get("/home/selectReviews",{id:getQueryString("id")},function (data) {
        for(var i=0;i<data.length;i++){
            if(type===0){
                var review=$("<div class='review' rid='"+data[i].id+"'>\n" +
                    "    <p class='text-right'>\n" +
                    "        <button class='btn btn-sm btn-danger'>\n" +
                    "            <span class='glyphicon glyphicon-remove'></span>\n" +
                    "        </button>\n" +
                    "    </p>\n" +
                    "    <div class='introduce'>\n"+
                    "        <h3 class='text-center'>\n" +
                    "            <span class='review-title'>"+data[i].title+"</span>\n" +
                    "        </h3>\n" +
                    "        <p class='review-content'>"+data[i].content.replace(new RegExp("\n","g"),"<br>")+"</p>\n" +
                    "        <p class='text-right'>\n" +
                    "            <button class='btn btn-sm btn-default'>\n" +
                    "                <span class='glyphicon glyphicon-pencil'></span>\n" +
                    "            </button>\n" +
                    "        </p>\n" +
                    "    </div>\n"+
                    "</div>");

                review.find(".btn-default").click(editorReview);
                review.find(".btn-danger").click(removeReview);
            }else {
                var review=$("<div class='review' rid='"+data[i].id+"'>\n" +
                    "    <div class='introduce'>\n"+
                    "        <h3 class='text-center'>\n" +
                    "            <span class='review-title'>"+data[i].title+"</span>\n" +
                    "        </h3>\n" +
                    "        <p class='review-content'>"+data[i].content.replace(new RegExp("\n","g"),"<br>")+"</p>\n" +
                    "    </div>\n"+
                    "</div>");
            }

            $("#review").append(review);
        }
    });
}

//获取个人信息
function getProfileMessage(type) {
    $.get("/home/selectProfileDetail",{id:getQueryString("id")},function (data) {
        if(type===3){
            profileMessageFrames(3,$("#profile"));
        }else {
            if(data["priority"]!==undefined){
                profileMessageFrames(0,$("#profile"));
            }else {
                profileMessageFrames(1,$("#profile"));
            }
        }

        for(var key in data){
            if(key=="image"){
                $("#img-profile").attr("src",data[key]);
            }else if(key=="city"||key=="language"){
                //先清空列表
                $("#"+key).find(".introduce-container").text("");
                for(var i=0;i<data[key].length;i++){
                    var text=$("<span cid='"+data[key][i].id+"'>"+data[key][i].data+"</span>");
                    $("#"+key).find(".introduce-container").append(text);
                }
            }else {
                $("#"+key).find(".introduce-container").html(data[key].replace(new RegExp("\n","g"),"<br>"));
            }
        }
    });
}

//写入翻译员的个人信息框架
function profileMessageFrames(type,parent) {

    switch (type){
        case 0:{
            parent.append(backgroundIntroduce("glyphicon-sort","Priority","priority"));
        }
        case 1:{
            parent.append(introduce("glyphicon-certificate","ID","id"));
            parent.append(backgroundIntroduce("glyphicon-lock","Password","pwd"));
            parent.append(backgroundIntroduce("glyphicon-user","Name","name"));
            parent.append(backgroundIntroduce("glyphicon-asterisk","Sex","sex"));
            parent.append(backgroundIntroduce("glyphicon-home","City","city"));
            parent.append(backgroundIntroduce("glyphicon-comment","Language","language"));
            parent.append(backgroundIntroduce("glyphicon-education","Education","educational"));
            parent.append(backgroundIntroduce("glyphicon-briefcase","Translation experience","translation_experience"));
            parent.append(backgroundIntroduce("glyphicon-book","Language proficiency","language_proficiency"));
            parent.append(backgroundIntroduce("glyphicon-yen","Payment","payment"));
            break;
        }
        case 2:{
            parent.append(introduce("glyphicon-user","Name","name"));
            parent.append(introduce("glyphicon-asterisk","Sex","sex"));
            parent.append(introduce("glyphicon-home","City","city"));
            parent.append(introduce("glyphicon-comment","Language","language"));
            parent.append(introduce("glyphicon-education","Education","educational"));
            parent.append(introduce("glyphicon-briefcase","Translation experience","translation_experience"));
            parent.append(introduce("glyphicon-book","Language proficiency","language_proficiency"));
            parent.append(introduce("glyphicon-yen","Payment","payment"));
            break;
        }
        case 3:{
            parent.append(introduce("glyphicon-certificate","ID","id"));
            parent.append(introduce("glyphicon-lock","Password","pwd"));
            parent.append(introduce("glyphicon-user","Name","name"));
            parent.append(introduce("glyphicon-asterisk","Sex","sex"));
            parent.append(introduce("glyphicon-home","City","city"));
            parent.append(introduce("glyphicon-comment","Language","language"));
            parent.append(introduce("glyphicon-education","Education","educational"));
            parent.append(introduce("glyphicon-briefcase","Translation experience","translation_experience"));
            parent.append(introduce("glyphicon-book","Language proficiency","language_proficiency"));
            parent.append(introduce("glyphicon-yen","Payment","payment"));
            break;
        }
    }
}

//生成一项翻译者表单
function introduce(icon,name,type) {
    return "<div class='introduce row' id='"+type+"'>\n" +
        "    <!--图标和标题-->\n" +
        "    <div class='col-sm-3'>\n" +
        "        <div class='col-sm-2'>\n" +
        "            <span class='glyphicon "+icon+"'></span>\n" +
        "        </div>\n" +
        "        <div class='col-sm-8'>\n" +
        "            <p><strong>"+name+"</strong></p>\n" +
        "        </div>\n" +
        "        <div class='clearfix'></div>\n" +
        "    </div>\n" +
        "    <!--内容块-->\n" +
        "    <div class='col-sm-9 introduce-container'></div>\n" +
        "    <div class='clearfix'></div>\n" +
        "</div>"
}

//生成一项后台管理翻译者表单
function backgroundIntroduce(icon, name, type) {
    return "<div id='"+type+"' class='introduce row'>\n" +
        "<!--图标和标题-->\n" +
        "<div class='col-sm-3'>\n" +
        "    <div class='col-sm-2'>\n" +
        "        <span class='glyphicon "+icon+"'></span>\n" +
        "    </div>\n" +
        "    <div class='col-sm-6'>\n" +
        "        <p><strong>"+name+"</strong></p>\n" +
        "    </div>\n" +
        "    <div class='col-sm-2'>\n" +
        "        <button class='btn btn-default'><span class='glyphicon glyphicon-pencil'></span></button>\n" +
        "    </div>\n" +
        "    <div class='clearfix'></div>\n" +
        "</div>\n" +
        "<!--内容块-->\n" +
        "<div class='col-sm-9 introduce-container'></div>\n" +
        "</div>"
}